//
//  PublishButton.m
//  Find
//
//  Created by lanou3g on 15/10/27.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "PublishButton.h"
#import "PublishView.h"


@interface PublishButton () {
    CGFloat _buttonImageHeight;
}
@property(nonatomic, retain) UIView *publishView;
@end
@implementation PublishButton
+(void)load {
    [super registerSubclass];
}

#pragma mark -
#pragma mark - Life Cycle

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.adjustsImageWhenHighlighted = NO;
    }
    
    return self;
}


//上下结构的 button
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 控件大小,间距大小
    // 大小
    CGFloat const imageViewEdge   = self.bounds.size.width * 0.8;
    CGFloat const centerOfView    = self.bounds.size.width * 0.5;
    CGFloat const labelLineHeight = self.titleLabel.font.lineHeight;
    // 距离上面的高度
    CGFloat const verticalMarginT = self.bounds.size.height - labelLineHeight - 10;
    CGFloat const verticalMargin  = verticalMarginT / 2;
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  = verticalMargin + imageViewEdge * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdge  + verticalMargin * 2 + labelLineHeight * 0.5 + 5;
    //imageView position 位置
    
    self.imageView.bounds = CGRectMake(0, 0, imageViewEdge, imageViewEdge);
    self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
    
    //title position 位置
    
    self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
    self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}


#pragma mark - Public Methods
/*
 *
 Create a custom UIButton with title and add it to the center of our tab bar
 *
 */
+ (instancetype)plusButton{
    
    PublishButton *button = [[PublishButton alloc]init];
    
    [button setImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
    [button setTitle:@"发布" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:9.5];
    [button sizeToFit];
    
    [button addTarget:button action:@selector(clickPublish:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return button;
}
#pragma mark -
#pragma mark - Event Response

- (void)clickPublish:(UIButton *)button {
  
    
    if (!self.publishView) {
        self.publishView =  [PublishView sharePublishView];
        [self.window addSubview:self.publishView];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            self.publishView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            
        }];

    }
    [[PublishView sharePublishView].cancelButton addTarget:self action:@selector(actionCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    [[PublishView sharePublishView].writeFindButton addTarget:self action:@selector(actionWriteFindButton:) forControlEvents:UIControlEventTouchUpInside];
    [[PublishView sharePublishView].writeLostButton addTarget:self action:@selector(actionWriteLostButton:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)actionCancelButton:(UIButton *)button
{
    [UIView animateWithDuration:0.3 animations:^{
        [PublishView sharePublishView].frame = CGRectMake(0, kScreenHeight, kScreenWidth, 0);
       
    }];
    
}
- (void)actionWriteLostButton:(UIButton *)button
{
   // [UIView animateWithDuration:0.2 animations:^{
        [PublishView sharePublishView].frame = CGRectMake(0, kScreenHeight, kScreenWidth, 0);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:nil userInfo:@{@"type":@"lost"}];
 //   }];
    
}

- (void)actionWriteFindButton:(UIButton *)button
{
    [UIView animateWithDuration:0.2 animations:^{
        [PublishView sharePublishView].frame = CGRectMake(0, kScreenHeight, kScreenWidth, 0);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONE" object:nil userInfo:@{@"type":@"find"}];
    }];
    
    

}
+ (CGFloat)multiplerInCenterY {
    return  0.3;
}



@end
